package {	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import mochi.as3.*;
	import flash.desktop.NativeApplication;
	import com.flashdynamix.utils.SWFProfiler;
	
	public class Main extends MovieClip {
		static public const SCALEY:Number = 0.8;
		
		private var scorePanel:MovieClip = new ScorePanel();
		private var timePanel:MovieClip = new TimePanel();		
		
		private var bg:MovieClip = new Background();
		private var balanceBar:MovieClip = scorePanel.balance;
		private var _balance:int;
		private var goodPowerBar:MovieClip = new GoodPower();
		private var _goodPower:uint;
		private var evilPowerBar:MovieClip = new EvilPower();
		private var _evilPower:uint;
		
		private var gameLevel:uint;
		private var gameExp:uint;
		private var gameExpTable:Array = [300, 600, 1000, 1100, 1200, 1300, 1500];
		
		private var _score:int;						
		
		private var persons:Array;
		private var switchs:Array;
		
		private var _combo:uint = 0;
		private var comboTimmer:uint = 0;
		
		private var initTime:Number;
		
		var soundChannel:SoundChannel = new SoundChannel();
		
		private var game:Game;
		
		//var game:Game =  new Game();						
		
		public function Main() {			
			//MochiServices.connect("21a72f35919ad262", root);												
			stage.quality = StageQuality.LOW;
			stop();
			SWFProfiler.init(stage, this);
			SWFProfiler.show();
			
			//addChild(game);
			
			this.scaleY = SCALEY;
			
			playMusic(null);
			soundChannel.addEventListener(Event.SOUND_COMPLETE, playMusic);
			addEventListener(Event.DEACTIVATE, flashDeactivated);
			
			drawMenu();												
		}		
		
		private function flashDeactivated(e:Event):void 
		{
			NativeApplication.nativeApplication.exit();
		}
		
		private function playMusic(e:Event):void 
		{
			soundChannel = new GameMusic().play();			
			soundChannel.addEventListener(Event.SOUND_COMPLETE, playMusic);
		}
		
		var txts:Array;
		private function drawMenu():void 
		{
			var mm:MovieClip = new MainMenu();						
			
			txts = [mm.start, mm.credits, mm.help];
						
			for (var i:uint = 0; i < txts.length; i++ ) {
				setUpTextFild(txts[i]);	
			}			
			
			txts[0].addEventListener(MouseEvent.CLICK, startGame);
			txts[1].addEventListener(MouseEvent.CLICK, creditsScreen);
			txts[2].addEventListener(MouseEvent.CLICK, helpScreen);
			
			addChild(mm);
		}
		
		private function startGame(e:MouseEvent):void 
		{
			new MenuSelectSound().play();			
			
			removeChild(e.currentTarget.parent);
			//for (var i:uint = 0; i < txts.length; i++ ) {
				//txts[i].visible = false;
			//}		
			
			//if(game.persons != null) for (var i:uint = 0; i < game.persons.length; i++ ) game.removeChild(game.persons[i]);
			game  = new Game();
			addChild(game); 
			game.initGame();			
		}
		
		private function setUpTextFild(txt:TextField):void 
		{
			txt.addEventListener(MouseEvent.MOUSE_OVER, highligthText);
			txt.addEventListener(MouseEvent.MOUSE_OUT, unHighlightText);	
		}
		
		private function creditsScreen(e:MouseEvent):void 
		{
			new MenuSelectSound().play();
			removeChild(e.currentTarget.parent);
			
			var cm:MovieClip = new CreditsMenu();
			
			addChild(cm);
			
			var back:TextField = cm.back;
			
			curColor = 0xFFFFFF;
			setUpTextFild(back);
			
			back.addEventListener(MouseEvent.CLICK, backToMainMenu);
		}
		
		private function helpScreen(e:MouseEvent):void 
		{			
			new MenuSelectSound().play();
			removeChild(e.currentTarget.parent);
			
			var hm:MovieClip = new HelpMenu();
			
			addChild(hm);
			
			var back:TextField = hm.back;
			
			curColor = 0xFFFFFF;
			setUpTextFild(back);
			
			back.addEventListener(MouseEvent.CLICK, backToMainMenu);
		}
		
		private function backToMainMenu(e:MouseEvent):void 
		{
			curColor = 0x000000;
			new MenuSelectSound().play();
			removeChild(e.currentTarget.parent as DisplayObject);
			
			drawMenu();
		}
		
		private var curColor:uint = 0x000000;
		private function highligthText(e:MouseEvent):void 
		{
			new MenuOverSound().play();
			e.currentTarget.textColor = 0xFFFF00;
			Mouse.cursor = MouseCursor.BUTTON;
		}
		
		private function unHighlightText(e:MouseEvent):void 
		{
			//new NavigationSound().play();
			e.currentTarget.textColor = curColor;
			Mouse.cursor = MouseCursor.AUTO;
		}				
		
	}	
}
